package com.google.code.chaotic.model;

import java.io.Serializable;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import java.util.Date;

//

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class HabitWaypoint implements Serializable {

	/**
	 * @uml.property name="sequenceNo"
	 */
	@Persistent
	private Integer sequenceNo;

	/**
	 * @uml.property name="commutingHabit"
	 * @uml.associationEnd 
	 *                     inverse="waypoints:com.google.code.chaotic.model.CommutingHabit"
	 */
	@Persistent
	private CommutingHabit commutingHabit;

	@Persistent
	private String commutingHabitId;

	/**
	 * @uml.property name="position"
	 */
	@Persistent
	@javax.jdo.annotations.Embedded(members = {
			@Persistent(name = "latitude", columns = @javax.jdo.annotations.Column(name = "posLat")),
			@Persistent(name = "longitude", columns = @javax.jdo.annotations.Column(name = "posLon")) })
	private LatLon position;

	// @PrimaryKey
	// public String getId() {
	// return new StringBuilder() //
	// .append(commutingHabit.getId()).append('-') //
	// .append(Integer.toHexString(sequenceNo)) //
	// .toString();
	// }
	//
	// public void setId(String id) {
	// // throw new UnsupportedOperationException();
	// if (id == null) {
	// this.sequenceNo = null;
	// return;
	// }
	// String sequenceStr = id.substring(id.lastIndexOf('-') + 1);
	// this.sequenceNo = Integer.parseInt(sequenceStr, 16);
	// }

	/** @uml.property name="gaeId" */
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String gaeId;

	public void setGaeId(String gaeId) {
		this.gaeId = gaeId;
	}

	public String getGaeId() {
		return gaeId;
	}

	@Persistent
	private Date version;

	//

	/** @uml.property name="sequenceNo" */
	public Integer getSequenceNo() {
		return sequenceNo;
	}

	/** @uml.property name="sequenceNo" */
	public void setSequenceNo(Integer sequenceNo) {
		this.sequenceNo = sequenceNo;
	}

	/** @uml.property name="position" */
	public LatLon getPosition() {
		return position;
	}

	/** @uml.property name="position" */
	public void setPosition(LatLon position) {
		this.position = position;
	}

	/** @uml.property name="commutingHabit" */
	public CommutingHabit getCommutingHabit() {
		return commutingHabit;
	}

	/** @uml.property name="commutingHabit" */
	public void setCommutingHabit(CommutingHabit commutingHabit) {
		this.commutingHabit = commutingHabit;
		commutingHabitId = //
		(commutingHabit != null ? commutingHabit.getId() : null);
	}

	public void setCommutingHabitId(String habitId) {
		this.commutingHabitId = habitId;
		if (commutingHabit != null
				&& !commutingHabit.getId().equals(habitId))
			commutingHabit = null;
	}

	public String getCommutingHabitId() {
		return commutingHabit != null ? commutingHabit.getId()
				: commutingHabitId;
	}

	/** @uml.property name="version" */
	public Date getVersion() {
		return version;
	}

	/** @uml.property name="version" */
	public void setVersion(Date version) {
		this.version = version;
	}
}
